<?php
	/** 收藏房源类
	@author ffpan
	@date 2010-04-10
	@version 1.0
	**/

	class Collection{
		function __construct(){
			$this->fangDb = new wbDatabase();
		}

		function __destruct(){}

		function __get($att){
			return $att;
		}

		function __set($att , $val){
			$this->$att = $val;
		}

		function safeVal($val){
			if (!get_magic_quotes_gpc()) {
				 $val = addslashes($val);
			}
			return $val;
		}

		function safePostVal(){
			if (!get_magic_quotes_gpc()) {
			   foreach($_POST as &$v){
				 $v = addslashes($v);
			   }
			}
			return $val;
		}
		
		
		
		//发送一条消息
		function addOneCollection(){
			$this->safePostVal();		
			$hid = $this->safeVal($_GET['hid']);
			$type = $this->safeVal($_GET["type"]);
			$userid = __user_prop__==0?"a_".__user_id__:"u_".__user_id__;
			
			$sql = "select id from ".__table_collection__." where hid=".$hid." and type='".$type."' and userid='".$userid."' ";
			$result = $this->fangDb->query($sql);
			if($result->num_rows>0){
				return "exist";
			}

			$sql = "insert into ".__table_collection__." 
					values(
					'',
					'".$hid."',
					'".$type."',
					'".$userid."'
					)";
			$result = $this->fangDb->query($sql);
			if($result){
				return "success";
			}else{
				return "fail";
			}
		}

		//获取信息列表
		function getCollectionList($type){
			$userid = __user_prop__==0?"a_".__user_id__:"u_".__user_id__;
			$sql = "select id , hid from ".__table_collection__." where userid='".$userid."' and type='".$type."' order by id desc";
			$table = $type=="s"?__table_sellhouse__:__table_renthouse__;

			$result = $this->fangDb->query($sql);
			$arr1 = array();
			$arr2 = array();
			if($result){
				while($row = $result->fetch_array()){
					$arr1[] = "id=".$row[1];
					$arr2[] = $row[0];
				}
			}
			$result->close();
			if(count($arr1)==0){return array();}
			$where = implode(" or " , $arr1);
			$sql = "select id , title , plaza , style , updatedate from ".$table." where ".$where;
			$result = $this->fangDb->query($sql);
			if($result){
				$i=0;
				while($row = $result->fetch_array()){
					$row["link"] = $type=="s"?"sellhouse_detail.php?sid=".$row["id"]:"renthouse_detail.php?rid=".$row["id"];
					$row["updatedate"] = $this->getshowtime($row["updatedate"]);
					$row["style_detail"] = $this->getStyleDetail($row["style"] , $row["mstyle"]);
					$row["cid"] = $arr2[$i][0];
					$arr[] = $row;
					$i++;
				}
			}
			return $arr;
		}
	
	
			
		//删除一条消息
		function deleteOneCollection(){
			$cid = $this->safeVal($_GET["cid"]);
			$sql = "delete from ".__table_collection__." where id=".$cid;
			$result = $this->fangDb->query($sql);
			if($result){
				return "success";
			}else{
				return "fail";
			}
		}

		
		function getStyleDetail($s , $ms){
			$str = "";
			$sstr = "";
			$sarr = explode("/" , $s);
			$str = $sarr[0]."室". $sarr[1]."厅". $sarr[2]."卫";
			$msarr = explode("|" , $ms);
			if($msarr[0]=="ck"){$sstr="车";}
			if($msarr[1]=="gl"){$sstr="阁";}
			if($sstr!=""){
				$str .= "(".$sstr.")";
			}
			return $str;
		}

		function getshowtime($time){
			$t = explode(" " , $time);
			$t0 = explode("-" , $t[0]);
			$t1= explode(":" , $t[1]);

			return ($t0[1]."-".$t0[2]." ".$t1[0].":".$t1[1]);
		}


	}

	$myCollection = new Collection();
?>